package com.findmeastore.action;

import java.util.List;

import com.findmeastore.bc.ICategoryBc;
import com.findmeastore.bo.CategoryBo;
import com.findmeastore.dto.CategoryDto;

public class CategoryCrudAction {
	private Integer id;
	private CategoryDto categoryDto;
	private List<CategoryBo> categories;
	private ICategoryBc categoryBc;
	private String action;
	
	public String execute() {
		if ("save".equals(action)) {
			return save();
		} else if ("list".equals(action)) {
			return list();
		} else if ("edit".equals(action)) {
			return edit();
		}
		return list();
	}
	
	public String list() {
		setCategories(categoryBc.getAllCategories());
		return "LIST";
	}

	public String edit() {
		if (id != null && id > 0) {
			CategoryBo categoryBo = categoryBc.getCategoryById(id);
			if (categoryBo != null) {
				categoryDto = new CategoryDto();
				categoryDto.setId(id);
				categoryDto.setName(categoryBo.getName());
				categoryDto.setParentCategoryId(categoryBo.getParentCategoryId());
			}
		}
		setCategories(categoryBc.getAllCategories());
		return "EDIT";
	}

	public String save() {
		if (categoryDto.getId() == null) {
			categoryDto.setCreatedById(1);
		}
		categoryDto.setModifiedById(1);
		if(categoryDto.getParentCategoryId()==-1){
			categoryDto.setParentCategoryId(null);
		}
		categoryBc.saveCategory(categoryDto);
		return "REDIRECT";
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public CategoryDto getCategoryDto() {
		return categoryDto;
	}

	public void setCategoryDto(CategoryDto categoryDto) {
		this.categoryDto = categoryDto;
	}

	public List<CategoryBo> getCategories() {
		return categories;
	}

	public void setCategories(List<CategoryBo> categories) {
		this.categories = categories;
	}

	public ICategoryBc getCategoryBc() {
		return categoryBc;
	}

	public void setCategoryBc(ICategoryBc categoryBc) {
		this.categoryBc = categoryBc;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}
}
